/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

// NOTE boost/logging log.h must be included before qt headers.
// #include "log.h"

#include <QApplication>
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>

#include "log.h"
#include "gui.h"
#include "controller.h"
#include "const.h"
#include "config.h"
#include <getopt.h>

/**
	Path to the executable.
*/
std::string path_to_bin;

void usage() {
	std::cout << "USAGE: " << path_to_bin << " [options]" << std::endl;
	std::cout << "Options: " << std::endl;
	std::cout << "-h : Show this. " << std::endl;
	std::cout << "-v : Echo version. " << std::endl;
	std::cout << "-s : Auto-apply. " << std::endl; //FIXME: eh?
	std::cout << "-d : Download only be default. " << std::endl;
}

int main(int argc, char** argv) {

	path_to_bin = argv[0];
	
	Q_INIT_RESOURCE(resources);
	
	Logger::init((boost::filesystem::path(path_to_bin).parent_path() / LOGFILE ).string());
	LOG ("Log started")

	QApplication app(argc, argv);

	bool auto_start=false, download_only=false;
	std::string template_name;
	int flag;

	// Parse command line options
	while ((flag = getopt(argc, argv, "dahv")) != -1) {
		switch(flag) {
			case 'a':
				auto_start=true;
				break;
			case 'd':
				download_only=true;
				break;
			case 'v':
				std::cout << "Version: " << VERSION << std::endl;
				return 0;
			case 'h':
			default:
				usage();
				return 0;
		}
	}

	// Parse template arguments
	if (optind < argc) {
		template_name = std::string(argv[optind]);
	}

	Config::init((boost::filesystem::path(path_to_bin).parent_path() / CONF_FILE ).string().c_str(), &app);


	Controller *controller = new Controller();

	AutoMateUi* ui = new AutoMateUi;

 	app.connect(&app, SIGNAL(aboutToQuit()), &CONFIG, SLOT(sync()));
	app.connect(ui, SIGNAL(apply(std::vector<Version>, bool)), controller, SLOT(apply(std::vector<Version>, bool)));
	app.connect(controller, SIGNAL(dataReadProgress(int, int)), ui, SLOT(updateDataReadProgress(int, int)));
	app.connect(ui, SIGNAL(abort()), controller, SLOT(abort()));
	app.connect(controller, SIGNAL(finished(bool)), ui, SLOT(resetApplyAction()));
	app.connect(controller, SIGNAL(aborted()), ui, SLOT(resetApplyAction()));
	app.connect(controller, SIGNAL(status(QString)), ui, SLOT(updateStatusMessage(QString)));
	app.connect(controller, SIGNAL(currentProcess(QString)), ui, SLOT(updateProgressName(QString)));

	// Act on command line arguments
	ui->setDownloadOnly(download_only);
	if (template_name.length() > 0) {
		ui->loadTemplate(template_name);
	}

	ui->show();

	if (auto_start)
		ui->applySelected();

	return app.exec();
}
